/************************************************************************

								Heroes IV
					  Copyright 2000, The 3DO Company

	------------------------------------------------------------------
  							   metatile_texture_cache.cpp

	$Header: /heroes4/metatile_texture_cache.cpp $

	$NoKeywords: $

 ************************************************************************/

#include "precompiled.h"
#pragma warning( disable: 4786 )

#include "metatile_texture_cache.h"

#include <cassert>

#include "conversion_cache.h"
#include "elements_of.h"
#include "enum_operations.h"
#include "metatile_texture.h"
#include "terrain.h"

//---------------------------------------------------------------------------
// Unnamed namespace
//---------------------------------------------------------------------------

namespace
{

	//---------------------------------------------------------------------------
	//---------------------------------------------------------------------------
	typedef t_conversion_cache< t_metatile_texture_24, t_metatile_texture > t_metatile_texture_cache;

	//---------------------------------------------------------------------------
	//---------------------------------------------------------------------------
	struct t_texture_info
	{
		int									num_textures;
		t_metatile_texture_cache * const *	texture_cache_ptr_array;
	};

	//---------------------------------------------------------------------------
	//---------------------------------------------------------------------------
	typedef t_texture_info const * t_terrain_texture_info_array[ k_terrain_count ];
	t_terrain_texture_info_array const & get_terrain_texture_info_array()
	{
		static t_metatile_texture_cache g_water_1_1_cache( "water.1.1" );
		static t_metatile_texture_cache g_water_1_2_cache( "water.1.2" );
		static t_metatile_texture_cache g_water_2_1_cache( "water.2.1" );
		static t_metatile_texture_cache g_water_2_2_cache( "water.2.2" );
		static t_metatile_texture_cache g_grass_1_1_cache( "grass.1.1" );
		static t_metatile_texture_cache g_grass_1_2_cache( "grass.1.2" );
		static t_metatile_texture_cache g_grass_2_1_cache( "grass.2.1" );
		static t_metatile_texture_cache g_grass_2_2_cache( "grass.2.2" );
		static t_metatile_texture_cache g_rough_1_1_cache( "rough.1.1" );
		static t_metatile_texture_cache g_rough_1_2_cache( "rough.1.2" );
		static t_metatile_texture_cache g_rough_2_1_cache( "rough.2.1" );
		static t_metatile_texture_cache g_rough_2_2_cache( "rough.2.2" );
		static t_metatile_texture_cache g_swamp_1_1_cache( "swamp.1.1" );
		static t_metatile_texture_cache g_swamp_1_2_cache( "swamp.1.2" );
		static t_metatile_texture_cache g_swamp_2_1_cache( "swamp.2.1" );
		static t_metatile_texture_cache g_swamp_2_2_cache( "swamp.2.2" );
		static t_metatile_texture_cache g_lava_1_1_cache( "lava.1.1" );
		static t_metatile_texture_cache g_lava_1_2_cache( "lava.1.2" );
		static t_metatile_texture_cache g_lava_2_1_cache( "lava.2.1" );
		static t_metatile_texture_cache g_lava_2_2_cache( "lava.2.2" );
		static t_metatile_texture_cache g_snow_1_1_cache( "snow.1.1" );
		static t_metatile_texture_cache g_snow_1_2_cache( "snow.1.2" );
		static t_metatile_texture_cache g_snow_2_1_cache( "snow.2.1" );
		static t_metatile_texture_cache g_snow_2_2_cache( "snow.2.2" );
		static t_metatile_texture_cache g_dirt_1_1_cache( "dirt.1.1" );
		static t_metatile_texture_cache g_dirt_1_2_cache( "dirt.1.2" );
		static t_metatile_texture_cache g_dirt_2_1_cache( "dirt.2.1" );
		static t_metatile_texture_cache g_dirt_2_2_cache( "dirt.2.2" );
		static t_metatile_texture_cache g_subterranean_1_1_cache( "subterranean.1.1" );
		static t_metatile_texture_cache g_subterranean_1_2_cache( "subterranean.1.2" );
		static t_metatile_texture_cache g_subterranean_2_1_cache( "subterranean.2.1" );
		static t_metatile_texture_cache g_subterranean_2_2_cache( "subterranean.2.2" );
		static t_metatile_texture_cache g_water_river_1_cache( "river.water.1" );
		static t_metatile_texture_cache g_water_river_2_cache( "river.water.2" );
		static t_metatile_texture_cache g_lava_river_1_cache( "river.lava.1" );
		static t_metatile_texture_cache g_lava_river_2_cache( "river.lava.2" );
		static t_metatile_texture_cache g_ice_river_1_cache( "river.ice.1" );
		static t_metatile_texture_cache g_ice_river_2_cache( "river.ice.2" );
		static t_metatile_texture_cache g_sand_1_1_cache( "sand.1.1" );
		static t_metatile_texture_cache g_sand_1_2_cache( "sand.1.2" );
		static t_metatile_texture_cache g_sand_2_1_cache( "sand.2.1" );
		static t_metatile_texture_cache g_sand_2_2_cache( "sand.2.2" );
		static t_metatile_texture_cache g_magic_all_cache( "magic.all" );
		static t_metatile_texture_cache g_magic_life_cache( "magic.life" );
		static t_metatile_texture_cache g_magic_order_cache( "magic.order" );
		static t_metatile_texture_cache g_magic_death_1_cache( "magic.death.1" );
		static t_metatile_texture_cache g_magic_death_2_cache( "magic.death.2" );
		static t_metatile_texture_cache g_magic_chaos_1_cache( "magic.chaos.1" );
		static t_metatile_texture_cache g_magic_chaos_2_cache( "magic.chaos.2" );
		static t_metatile_texture_cache g_magic_nature_1_cache( "magic.nature.1" );
		static t_metatile_texture_cache g_magic_nature_2_cache( "magic.nature.2" );
		static t_metatile_texture_cache g_might_1_cache( "special.personal_combat.1" );
		static t_metatile_texture_cache g_might_2_cache( "special.personal_combat.2" );

		static t_metatile_texture_cache * const k_water_1_texture_cache_array[] = { &g_water_1_1_cache, &g_water_1_2_cache };
		static t_metatile_texture_cache * const k_water_2_texture_cache_array[] = { &g_water_2_1_cache, &g_water_2_2_cache };
		static t_metatile_texture_cache * const k_grass_1_texture_cache_array[] = { &g_grass_1_1_cache, &g_grass_1_2_cache };
		static t_metatile_texture_cache * const k_grass_2_texture_cache_array[] = { &g_grass_2_1_cache, &g_grass_2_2_cache };
		static t_metatile_texture_cache * const k_rough_1_texture_cache_array[] = { &g_rough_1_1_cache, &g_rough_1_2_cache };
		static t_metatile_texture_cache * const k_rough_2_texture_cache_array[] = { &g_rough_2_1_cache, &g_rough_2_2_cache };
		static t_metatile_texture_cache * const k_swamp_1_texture_cache_array[] = { &g_swamp_1_1_cache, &g_swamp_1_2_cache };
		static t_metatile_texture_cache * const k_swamp_2_texture_cache_array[] = { &g_swamp_2_1_cache, &g_swamp_2_2_cache };
		static t_metatile_texture_cache * const k_lava_1_texture_cache_array[] = { &g_lava_1_1_cache, &g_lava_1_2_cache };
		static t_metatile_texture_cache * const k_lava_2_texture_cache_array[] = { &g_lava_2_1_cache, &g_lava_2_2_cache };
		static t_metatile_texture_cache * const k_snow_1_texture_cache_array[] = { &g_snow_1_1_cache, &g_snow_1_2_cache };
		static t_metatile_texture_cache * const k_snow_2_texture_cache_array[] = { &g_snow_2_1_cache, &g_snow_2_2_cache };
		static t_metatile_texture_cache * const k_sand_1_texture_cache_array[] = { &g_sand_1_1_cache, &g_sand_1_2_cache };
		static t_metatile_texture_cache * const k_sand_2_texture_cache_array[] = { &g_sand_2_1_cache, &g_sand_2_2_cache };
		static t_metatile_texture_cache * const k_dirt_1_texture_cache_array[] = { &g_dirt_1_1_cache, &g_dirt_1_2_cache };
		static t_metatile_texture_cache * const k_dirt_2_texture_cache_array[] = { &g_dirt_2_1_cache, &g_dirt_2_2_cache };
		static t_metatile_texture_cache * const k_subterranean_1_texture_cache_array[] = { &g_subterranean_1_1_cache, &g_subterranean_1_2_cache };
		static t_metatile_texture_cache * const k_subterranean_2_texture_cache_array[] = { &g_subterranean_2_1_cache, &g_subterranean_2_2_cache };
		static t_metatile_texture_cache * const k_water_river_texture_cache_array[] = { &g_water_river_1_cache, &g_water_river_2_cache };
		static t_metatile_texture_cache * const k_lava_river_texture_cache_array[] = { &g_lava_river_1_cache, &g_lava_river_2_cache };
		static t_metatile_texture_cache * const k_ice_river_texture_cache_array[] = { &g_ice_river_1_cache, &g_ice_river_2_cache };
		static t_metatile_texture_cache * const k_magic_all_texture_cache_array[] = { &g_magic_all_cache };
		static t_metatile_texture_cache * const k_magic_life_texture_cache_array[] = { &g_magic_life_cache };
		static t_metatile_texture_cache * const k_magic_order_texture_cache_array[] = { &g_magic_order_cache };
		static t_metatile_texture_cache * const k_magic_death_texture_cache_array[] = { &g_magic_death_1_cache, &g_magic_death_2_cache };
		static t_metatile_texture_cache * const k_magic_chaos_texture_cache_array[] = { &g_magic_chaos_1_cache, &g_magic_chaos_2_cache };
		static t_metatile_texture_cache * const k_magic_nature_texture_cache_array[] = { &g_magic_nature_1_cache, &g_magic_nature_2_cache };
		static t_metatile_texture_cache * const k_might_texture_cache_array[] = { &g_might_1_cache, &g_might_2_cache };

		static t_texture_info const k_water_texture_info_array[ k_terrain_water_subtype_count ] =
		{
			{ ELEMENTS_OF( k_water_1_texture_cache_array ), k_water_1_texture_cache_array },
			{ ELEMENTS_OF( k_water_2_texture_cache_array ), k_water_2_texture_cache_array },
		};

		static t_texture_info const k_grass_texture_info_array[ k_terrain_grass_subtype_count ] =
		{
			{ ELEMENTS_OF( k_grass_1_texture_cache_array ), k_grass_1_texture_cache_array },
			{ ELEMENTS_OF( k_grass_2_texture_cache_array ), k_grass_2_texture_cache_array },
		};

		static t_texture_info const k_rough_texture_info_array[ k_terrain_rough_subtype_count ] =
		{
			{ ELEMENTS_OF( k_rough_1_texture_cache_array ), k_rough_1_texture_cache_array },
			{ ELEMENTS_OF( k_rough_2_texture_cache_array ), k_rough_2_texture_cache_array },
		};

		static t_texture_info const k_swamp_texture_info_array[ k_terrain_swamp_subtype_count ] =
		{
			{ ELEMENTS_OF( k_swamp_1_texture_cache_array ), k_swamp_1_texture_cache_array },
			{ ELEMENTS_OF( k_swamp_2_texture_cache_array ), k_swamp_2_texture_cache_array },
		};

		static t_texture_info const k_lava_texture_info_array[ k_terrain_lava_subtype_count ] =
		{
			{ ELEMENTS_OF( k_lava_1_texture_cache_array ), k_lava_1_texture_cache_array },
			{ ELEMENTS_OF( k_lava_2_texture_cache_array ), k_lava_2_texture_cache_array },
		};

		static t_texture_info const k_snow_texture_info_array[ k_terrain_snow_subtype_count ] =
		{
			{ ELEMENTS_OF( k_snow_1_texture_cache_array ), k_snow_1_texture_cache_array },
			{ ELEMENTS_OF( k_snow_2_texture_cache_array ), k_snow_2_texture_cache_array },
		};

		static t_texture_info const k_sand_texture_info_array[ k_terrain_sand_subtype_count ] =
		{
			{ ELEMENTS_OF( k_sand_1_texture_cache_array ), k_sand_1_texture_cache_array },
			{ ELEMENTS_OF( k_sand_2_texture_cache_array ), k_sand_2_texture_cache_array },
		};

		static t_texture_info const k_dirt_texture_info_array[ k_terrain_dirt_subtype_count ] =
		{
			{ ELEMENTS_OF( k_dirt_1_texture_cache_array ), k_dirt_1_texture_cache_array },
			{ ELEMENTS_OF( k_dirt_2_texture_cache_array ), k_dirt_2_texture_cache_array },
		};

		static t_texture_info const k_subterranean_texture_info_array[ k_terrain_subterranean_subtype_count ] =
		{
			{ ELEMENTS_OF( k_subterranean_1_texture_cache_array ), k_subterranean_1_texture_cache_array },
			{ ELEMENTS_OF( k_subterranean_2_texture_cache_array ), k_subterranean_2_texture_cache_array },
		};

		static t_texture_info const k_water_river_texture_info_array[ k_terrain_water_river_subtype_count ] =
		{
			{ ELEMENTS_OF( k_water_river_texture_cache_array ), k_water_river_texture_cache_array },
		};

		static t_texture_info const k_lava_river_texture_info_array[ k_terrain_lava_river_subtype_count ] =
		{
			{ ELEMENTS_OF( k_lava_river_texture_cache_array ), k_lava_river_texture_cache_array },
		};

		static t_texture_info const k_ice_river_texture_info_array[ k_terrain_ice_river_subtype_count ] =
		{
			{ ELEMENTS_OF( k_ice_river_texture_cache_array ), k_ice_river_texture_cache_array },
		};

		static t_texture_info const k_magic_all_texture_info_array[ k_terrain_magic_all_subtype_count ] =
		{
			{ ELEMENTS_OF( k_magic_all_texture_cache_array ), k_magic_all_texture_cache_array },
		};

		static t_texture_info const k_magic_life_texture_info_array[ k_terrain_magic_life_subtype_count ] =
		{
			{ ELEMENTS_OF( k_magic_life_texture_cache_array ), k_magic_life_texture_cache_array },
		};

		static t_texture_info const k_magic_order_texture_info_array[ k_terrain_magic_order_subtype_count ] =
		{
			{ ELEMENTS_OF( k_magic_order_texture_cache_array ), k_magic_order_texture_cache_array },
		};

		static t_texture_info const k_magic_death_texture_info_array[ k_terrain_magic_death_subtype_count ] =
		{
			{ ELEMENTS_OF( k_magic_death_texture_cache_array ), k_magic_death_texture_cache_array },
		};

		static t_texture_info const k_magic_chaos_texture_info_array[ k_terrain_magic_chaos_subtype_count ] =
		{
			{ ELEMENTS_OF( k_magic_chaos_texture_cache_array ), k_magic_chaos_texture_cache_array },
		};

		static t_texture_info const k_magic_nature_texture_info_array[ k_terrain_magic_nature_subtype_count ] =
		{
			{ ELEMENTS_OF( k_magic_nature_texture_cache_array ), k_magic_nature_texture_cache_array },
		};

		static t_texture_info const k_might_texture_info_array[ k_terrain_might_subtype_count ] =
		{
			{ ELEMENTS_OF( k_might_texture_cache_array ), k_might_texture_cache_array },
		};

		static t_texture_info const * const k_terrain_texture_info_array[ k_terrain_count ] =
		{
			k_water_texture_info_array,
			k_grass_texture_info_array,
			k_rough_texture_info_array,
			k_swamp_texture_info_array,
			k_lava_texture_info_array,
			k_snow_texture_info_array,
			k_sand_texture_info_array,
			k_dirt_texture_info_array,
			k_subterranean_texture_info_array,
			k_water_river_texture_info_array,
			k_lava_river_texture_info_array,
			k_ice_river_texture_info_array,
			k_magic_all_texture_info_array,
			k_magic_life_texture_info_array,
			k_magic_order_texture_info_array,
			k_magic_death_texture_info_array,
			k_magic_chaos_texture_info_array,
			k_magic_nature_texture_info_array,
			k_might_texture_info_array,
		};

		return k_terrain_texture_info_array;
	}

	//---------------------------------------------------------------------------
	//---------------------------------------------------------------------------
	inline t_texture_info const & get_terrain_texture_info( t_terrain_type terrain_type, int terrain_subtype )
	{
		static t_terrain_texture_info_array const & k_terrain_texture_info_array = get_terrain_texture_info_array();
		return k_terrain_texture_info_array[ terrain_type ][ terrain_subtype ];
	}

	//---------------------------------------------------------------------------
	//---------------------------------------------------------------------------
	typedef t_texture_info t_road_texture_info_array[ k_road_type_count - 1 ];
	t_road_texture_info_array const & get_road_texture_info_array()
	{
		static t_metatile_texture_cache g_road_1_1_cache( "road.gravel.1" );
		static t_metatile_texture_cache g_road_1_2_cache( "road.gravel.2" );
		static t_metatile_texture_cache g_road_2_1_cache( "road.dirt.1" );
		static t_metatile_texture_cache g_road_2_2_cache( "road.dirt.2" );
		static t_metatile_texture_cache g_road_3_1_cache( "road.cobblestone.1" );
		static t_metatile_texture_cache g_road_3_2_cache( "road.cobblestone.2" );

		static t_metatile_texture_cache * const k_road_1_texture_cache_array[] = { &g_road_1_1_cache, &g_road_1_2_cache };
		static t_metatile_texture_cache * const k_road_2_texture_cache_array[] = { &g_road_2_1_cache, &g_road_2_2_cache };
		static t_metatile_texture_cache * const k_road_3_texture_cache_array[] = { &g_road_3_1_cache, &g_road_3_2_cache };

		static t_texture_info const k_road_texture_info_array[ k_road_type_count - 1 ] =
		{
			{ ELEMENTS_OF( k_road_1_texture_cache_array ), k_road_1_texture_cache_array },
			{ ELEMENTS_OF( k_road_2_texture_cache_array ), k_road_2_texture_cache_array },
			{ ELEMENTS_OF( k_road_3_texture_cache_array ), k_road_3_texture_cache_array },
		};

		return k_road_texture_info_array;
	}

	//---------------------------------------------------------------------------
	//---------------------------------------------------------------------------
	t_texture_info const & get_road_texture_info( t_road_type road_type )
	{
		static t_road_texture_info_array const & k_road_texture_info_array = get_road_texture_info_array();
		return k_road_texture_info_array[ road_type - 1 ];
	}

	//---------------------------------------------------------------------------
	//---------------------------------------------------------------------------
	typedef std::vector< t_metatile_texture_ptr > * t_terrain_texture_ptr_array[ k_terrain_count ];
	t_terrain_texture_ptr_array & get_terrain_texture_ptr_array()
	{
		static std::vector< t_metatile_texture_ptr > water_texture_ptr_array[ k_terrain_water_subtype_count ];
		static std::vector< t_metatile_texture_ptr > grass_texture_ptr_array[ k_terrain_grass_subtype_count ];
		static std::vector< t_metatile_texture_ptr > rough_texture_ptr_array[ k_terrain_rough_subtype_count ];
		static std::vector< t_metatile_texture_ptr > swamp_texture_ptr_array[ k_terrain_swamp_subtype_count ];
		static std::vector< t_metatile_texture_ptr > lava_texture_ptr_array[ k_terrain_lava_subtype_count ];
		static std::vector< t_metatile_texture_ptr > snow_texture_ptr_array[ k_terrain_snow_subtype_count ];
		static std::vector< t_metatile_texture_ptr > sand_texture_ptr_array[ k_terrain_sand_subtype_count ];
		static std::vector< t_metatile_texture_ptr > dirt_texture_ptr_array[ k_terrain_dirt_subtype_count ];
		static std::vector< t_metatile_texture_ptr > subterranean_texture_ptr_array[ k_terrain_subterranean_subtype_count ];
		static std::vector< t_metatile_texture_ptr > water_river_texture_ptr_array[ k_terrain_water_river_subtype_count ];
		static std::vector< t_metatile_texture_ptr > lava_river_texture_ptr_array[ k_terrain_lava_river_subtype_count ];
		static std::vector< t_metatile_texture_ptr > ice_river_texture_ptr_array[ k_terrain_ice_river_subtype_count ];
		static std::vector< t_metatile_texture_ptr > magic_all_texture_ptr_array[ k_terrain_magic_all_subtype_count ];
		static std::vector< t_metatile_texture_ptr > magic_life_texture_ptr_array[ k_terrain_magic_life_subtype_count ];
		static std::vector< t_metatile_texture_ptr > magic_order_texture_ptr_array[ k_terrain_magic_order_subtype_count ];
		static std::vector< t_metatile_texture_ptr > magic_death_texture_ptr_array[ k_terrain_magic_death_subtype_count ];
		static std::vector< t_metatile_texture_ptr > magic_chaos_texture_ptr_array[ k_terrain_magic_chaos_subtype_count ];
		static std::vector< t_metatile_texture_ptr > magic_nature_texture_ptr_array[ k_terrain_magic_nature_subtype_count ];
		static std::vector< t_metatile_texture_ptr > might_texture_ptr_array[ k_terrain_might_subtype_count ];

		static std::vector< t_metatile_texture_ptr > * terrain_texture_ptr_array[ k_terrain_count ] =
		{
			water_texture_ptr_array,
			grass_texture_ptr_array,
			rough_texture_ptr_array,
			swamp_texture_ptr_array,
			lava_texture_ptr_array,
			snow_texture_ptr_array,
			sand_texture_ptr_array,
			dirt_texture_ptr_array,
			subterranean_texture_ptr_array,
			water_river_texture_ptr_array,
			lava_river_texture_ptr_array,
			ice_river_texture_ptr_array,
			magic_all_texture_ptr_array,
			magic_life_texture_ptr_array,
			magic_order_texture_ptr_array,
			magic_death_texture_ptr_array,
			magic_chaos_texture_ptr_array,
			magic_nature_texture_ptr_array,
			might_texture_ptr_array,
		};

		static bool first_time = true;
		assert( first_time ); // Should be called only once

		if ( first_time )
		{
			t_terrain_type terrain_type;
			for ( terrain_type = t_terrain_type( 0 ); terrain_type < k_terrain_count; enum_incr( terrain_type ) )
			{
				int subtype_count = get_terrain_subtype_count( terrain_type );
				int terrain_subtype;
				for ( terrain_subtype = 0; terrain_subtype < subtype_count; ++terrain_subtype )
				{
					int texture_count = get_terrain_texture_info( terrain_type, terrain_subtype ).num_textures;
					terrain_texture_ptr_array[ terrain_type ][ terrain_subtype ].resize( texture_count );
				}
			}

			first_time = false;
		}

		return terrain_texture_ptr_array;
	}

	//---------------------------------------------------------------------------
	//---------------------------------------------------------------------------
	inline t_metatile_texture_ptr & get_terrain_texture_ptr( t_terrain_type terrain_type, int terrain_subtype, int texture_num )
	{
		static t_terrain_texture_ptr_array & terrain_texture_ptr_array = get_terrain_texture_ptr_array();
		return terrain_texture_ptr_array[ terrain_type ][ terrain_subtype ][ texture_num ];
	}

	//---------------------------------------------------------------------------
	//---------------------------------------------------------------------------
	typedef std::vector< t_metatile_texture_ptr > t_road_texture_ptr_array[ k_road_type_count - 1 ];
	t_road_texture_ptr_array & get_road_texture_ptr_array()
	{
		static std::vector< t_metatile_texture_ptr > road_texture_ptr_array[ k_road_type_count - 1 ];

		static bool first_time = true;
		assert( first_time ); // Should be called only once

		if ( first_time )
		{
			t_road_type road_type;
			for ( road_type = t_road_type( 1 ); road_type < k_road_type_count; enum_incr( road_type ) )
			{
				int texture_count = get_road_texture_info( road_type ).num_textures;
				road_texture_ptr_array[ road_type - 1 ].resize( texture_count );
			}

			first_time = false;
		}

		return road_texture_ptr_array;
	}

	//---------------------------------------------------------------------------
	//---------------------------------------------------------------------------
	inline t_metatile_texture_ptr & get_road_texture_ptr( t_road_type road_type, int texture_num )
	{
		static t_road_texture_ptr_array & road_texture_ptr_array = get_road_texture_ptr_array();
		return road_texture_ptr_array[ road_type - 1 ][ texture_num ];
	}

} // Unnamed namespace

//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
int get_metatile_texture_count( t_road_type road_type )
{
	assert( road_type >= 0 && road_type < k_road_type_count );
	assert( road_type != k_road_none );

	return get_road_texture_info( road_type ).num_textures;
}

//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
int get_metatile_texture_count( t_terrain_type terrain_type, int terrain_subtype )
{
	assert( terrain_type >= 0 && terrain_type < k_terrain_count );
	assert( terrain_subtype >= 0 && terrain_subtype < get_terrain_subtype_count( terrain_type ) );

	return get_terrain_texture_info( terrain_type, terrain_subtype ).num_textures;
}

//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
t_metatile_texture_ptr get_metatile_texture( t_road_type road_type, int texture_num )
{
	assert( road_type >= 0 && road_type < k_road_type_count );
	assert( road_type != k_road_none );
	assert( texture_num >= 0 && texture_num < get_road_texture_info( road_type ).num_textures );

	t_metatile_texture_ptr & result = get_road_texture_ptr( road_type, texture_num );
	if ( result.get() == 0 )
		result = get_road_texture_info( road_type ).texture_cache_ptr_array[ texture_num ]->get();

	return result;
}

//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
t_metatile_texture_ptr get_metatile_texture( t_terrain_type terrain_type, int terrain_subtype, int texture_num )
{
	assert( terrain_type >= 0 && terrain_type < k_terrain_count );
	assert( terrain_subtype >= 0 && terrain_subtype < get_terrain_subtype_count( terrain_type ) );
	assert( texture_num >= 0 && texture_num < get_terrain_texture_info( terrain_type, terrain_subtype ).num_textures );

	t_metatile_texture_ptr & result = get_terrain_texture_ptr( terrain_type, terrain_subtype, texture_num );
	if ( result.get() == 0 )
		result = get_terrain_texture_info( terrain_type, terrain_subtype ).texture_cache_ptr_array[ texture_num ]->get();

	return result;
}
